package org.glut.config;

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    //认证
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers(("/level2/**")).hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");
        http.formLogin()
                .usernameParameter("username")
                .passwordParameter("password")
                .loginPage("/toLogin")
                .loginProcessingUrl("/login"); // 登陆表单提交请求
        http.logout().logoutSuccessUrl("/");
        http.csrf().disable();      //关闭csrf
        http.rememberMe().rememberMeParameter("remember");

    }

    @Override
    //授权
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //这些数据正常应该中数据库中读
       auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
               .withUser("admin").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3")
               .and().withUser("test").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");

    }
}
